package org.niux.aims.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.niux.aims.common.web.AimsBaseAction;
import org.niux.aims.service.IAuthService;
import org.niux.aims.service.ILogsService;
import org.niux.aims.service.IMenuService;
import org.niux.aims.vo.AccountVO;
import org.niux.aims.vo.AuthVO;
import org.niux.aims.vo.MenuVO;

/**
 * 
 * Copyright (c) 2009,niux<br>
 * All rights reserved.<br>
 * 
 * 文件名称：MainAction.java<br>
 * 摘 要：系统首页类<br>
 * <br>
 * 当前版本：<br>
 * 作 者：铭显 Email: <a
 * href="mailto:liangmingxian@gmail.com">liangmingxian@gmail.com</a><br>
 * 完成日期：@since 2013-7-30<br>
 * <br>
 * 取代版本：<br>
 * 原作者 ：<br>
 * 完成日期：@since Oct 22, 2009<br>
 */
public class MainAction extends AimsBaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8148055768369390440L;
	private Log log = LogFactory.getLog(getClass());

	@Resource(name="menuService")
	private IMenuService menuService;
	
	@Resource(name="authService")
	private IAuthService authService;
	
	@Resource(name="logsService")
	private ILogsService logsService;
	
	/**
	 * 
	 * 方法概述：页面渲染 
	 * 描述：页面渲染
	 * 
	 * @return 
	 * 返回类型：String 
	 * 修改人：梁铭显 
	 * 修改时间：2013-7-30
	 */
	public String showPage() {
		AccountVO account = ((AccountVO)super.getSession().get("account"));
		
		logsService.addLog("【"+account.getName()+"】进入系统");
		super.getRequest().setAttribute("account", account);

		// 获取菜单
		if (account.getName().equals("admin")) { // 最高用户
			log.debug("最高用户登录");
			List<MenuVO> menus = menuService.getMenusByEntityCodes(new ArrayList<String>());
			
			super.getRequest().setAttribute("menus", menus);
			super.getSession().put("auths", new ArrayList<String>());
		} else {
			//获取权限
			List<AuthVO> auths = authService.findByAccount(account.getId());
			List<String> menuCodes = new ArrayList<String>();
			
			for (AuthVO auth : auths) {
				menuCodes.add(auth.getEntity().getEntityCode());
			}
			
			List<MenuVO> menus = menuService.getMenusByEntityCodes(menuCodes);
			super.getRequest().setAttribute("menus", menus);
			super.getSession().put("auths", menuCodes);
		}
		
		//当前日期
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String currentDate = simpleDateFormat.format(new Date());
		super.getRequest().setAttribute("currentDate", currentDate);

		return "success";
	}
}
